package com.yishusmt.mobsmt.serviceimpl;

import com.yishusmt.mobsmt.constant.OrderConstant;
import com.yishusmt.mobsmt.dto.base.Result;
import com.yishusmt.mobsmt.dto.request.OrderDetailsReq;
import com.yishusmt.mobsmt.dto.request.SeekOrderReq;
import com.yishusmt.mobsmt.dto.response.OrderDetailsRes;
import com.yishusmt.mobsmt.dto.response.Tips;
import com.yishusmt.mobsmt.dto.response.UserRes;
import com.yishusmt.mobsmt.entity.OrderDetailsEntity;
import com.yishusmt.mobsmt.enums.ResultCodeEnum;
import com.yishusmt.mobsmt.mapper.OrderDetailsEntityMapper;
import com.yishusmt.mobsmt.mapper.OrderDetailsInfoMapper;
import com.yishusmt.mobsmt.mapper.UserInfoMapper;
import com.yishusmt.mobsmt.service.OrderService;
import com.yishusmt.mobsmt.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

import static com.yishusmt.mobsmt.constant.OrderConstant.ALL_ORDER;
import static com.yishusmt.mobsmt.constant.OrderConstant.BUSINESS_COMMISSIONER_PHONE;

/**
 * 订单服务 TODO
 *
 * @author zhangyi
 * @date 2018/8/11 14:13
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private OrderDetailsEntityMapper orderDetailsEntityMapper;

    @Autowired
    private OrderDetailsInfoMapper orderDetailsInfoMapper;

    /**
     * 分类查询订单信息
     *
     * @param status
     * @param phone
     * @return
     */
    @Override
    public Result ownOrderList(Integer status, String phone) {
        Result result = null;
        //分类查询实体
        SeekOrderReq seekOrderReq = new SeekOrderReq();
        seekOrderReq.setPhone(phone);
        List<OrderDetailsRes> orderDetailsRes = null;
        switch (status){
            case 0:
                orderDetailsRes = orderDetailsInfoMapper.selectAllOrder(phone);
                break;
            case 1:
                seekOrderReq.setStatus(1);
                orderDetailsRes = orderDetailsInfoMapper.seekOrderDetails(seekOrderReq);
                break;
            case 2:
                seekOrderReq.setStatus(0);
                orderDetailsRes = orderDetailsInfoMapper.seekOrderDetails(seekOrderReq);
                break;
            case 3:
                seekOrderReq.setStatus(2);
                orderDetailsRes = orderDetailsInfoMapper.seekOrderDetails(seekOrderReq);
                break;
                default:
                    return TipsUtil.returnTips(ResultCodeEnum.NULL_DATA,null);
        }
        result = new Result(JacksonUtil.toJSon(orderDetailsRes));
        return result;
    }

    /**
     * 获取订单详细信息
     *
     * @param orderId
     * @return
     */
    @Override
    public Result orderInfo(Long orderId) {
        OrderDetailsEntity orderDetailsEntity = orderDetailsEntityMapper.selectByPrimaryKey(orderId);
        return new Result(orderDetailsEntity);
    }

    /**
     * 用户下单
     *
     * @param orderDetailsReq
     * @return
     */
    @Override
    public Result placeAnOrder(OrderDetailsReq orderDetailsReq) {
        UserRes userRes = userInfoMapper.selectUserSelective(orderDetailsReq.getPhone());
        if (userRes != null && userRes.getId() != null) {
            //下单
            OrderDetailsEntity orderDetailsEntity = new OrderDetailsEntity();
            orderDetailsEntity.setUserId(userRes.getId());
            orderDetailsEntity.setBoardCount(orderDetailsReq.getBoardCount());
            orderDetailsEntity.setAoi(orderDetailsReq.getAoi());
            orderDetailsEntity.setDeliveryTime(orderDetailsReq.getDeliveryTime());
            orderDetailsEntity.setGenerationTime(orderDetailsReq.getGenerationTime());
            orderDetailsEntity.setMaterialGeneration(orderDetailsReq.getMaterialGeneration());
            orderDetailsEntity.setMounting(orderDetailsReq.getMounting());
            orderDetailsEntity.setRemark(orderDetailsReq.getRemark());
            orderDetailsEntity.setReceivingAddressId(orderDetailsReq.getReceivingAddressId());
            orderDetailsEntity.setProcessMaterial(orderDetailsReq.getProcessMaterial());


            //生成订单号
            UUID uuid = UUID.randomUUID();
            orderDetailsEntity.setOrderNo(userRes.getId() +  MD5Util.encryptString(uuid.toString()).substring(0,15));
            orderDetailsEntity.setCreateTime(DateUtil.getCurrentDate());
            orderDetailsEntity.setUpdateTime(DateUtil.getCurrentDate());

            int i = orderDetailsEntityMapper.insertSelective(orderDetailsEntity);
            if (0 == i) {
                return TipsUtil.returnTips(ResultCodeEnum.OPERATION_FAIL, "下单失败！");
            } else {
                //通知商务专员(假定死)
                AliyunSmsUtil.sendPlaceAnOrder(BUSINESS_COMMISSIONER_PHONE, userRes.getName() != null ? userRes.getName() : "用户");
                return TipsUtil.returnTips(ResultCodeEnum.OPERATION_SUCCESS, "我们已经处理订单了");
            }
        } else {
            return TipsUtil.returnTips(ResultCodeEnum.USER_LOGIN_FAIL, "用户信息不存在！");
        }
    }
}

